import socket
import time

def udp_client():
    # 创建UDP socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    # 服务器地址
    server_address = ('127.0.0.1', 12345)
    
    try:
        for num in [0, 1, 2]:
            # 发送数据
            client_socket.sendto(num.to_bytes(4, byteorder='little'), server_address)
            print(f"已发送数字: {num}")
            
            # 等待响应
            data, _ = client_socket.recvfrom(1024)
            received_num = int.from_bytes(data, byteorder='little')
            print(f"收到响应: {received_num}")
            
            time.sleep(1)  # 间隔1秒
            
    except KeyboardInterrupt:
        print("\n客户端正在关闭...")
    finally:
        client_socket.close()

if __name__ == '__main__':
    udp_client()